package com.txw.serviceedu.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.txw.serviceedu.entity.EduSubject;
import com.txw.serviceedu.entity.excel.SubjectData;
import com.txw.serviceedu.entity.subject.OneSubject;
import com.txw.serviceedu.entity.subject.TwoSubject;
import com.txw.serviceedu.listener.SubjectExelListener;
import com.txw.serviceedu.mapper.EduSubjectMapper;
import com.txw.serviceedu.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author TanXiongWei
 * @since 2021-11-26
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {


    /**
     * 添加
     *
     * @param file
     * @param subjectService
     */
    @Override
    public void saveSubject(MultipartFile file, EduSubjectService subjectService) {
        try {
            //文件输入流
            InputStream in = file.getInputStream();

            //调用方法进行读取
            EasyExcel.read(in, SubjectData.class, new SubjectExelListener(subjectService)).sheet().doRead();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    @Override
    public List<OneSubject> getAllOneTwoSubject() {

        //查询所有一级分类
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id", "0");
        List<EduSubject> ontEduSubjects = baseMapper.selectList(wrapperOne);

        //查询所有二级分类
        QueryWrapper<EduSubject> wrapperTow = new QueryWrapper<>();
        wrapperTow.ne("parent_id", "0");
        List<EduSubject> twoEduSubjects = baseMapper.selectList(wrapperTow);

        //创建list集合，用于存储最终封装数据
        List<OneSubject> finalSubjectList = new ArrayList<>();

        //封装一级分类
        for (int i = 0; i < ontEduSubjects.size(); i++) {

            EduSubject eduSubject = ontEduSubjects.get(i);
            OneSubject oneSubject = new OneSubject();
//            oneSubject.setId(eduSubject.getId());
//            oneSubject.setTitle(eduSubject.getTitle());
            BeanUtils.copyProperties(eduSubject, oneSubject);
            finalSubjectList.add(oneSubject);


            List<TwoSubject> twoFinalSubjectList = new ArrayList<>();

            //遍历二级分类list集合
            for (int t = 0; t < twoEduSubjects.size(); t++) {
                EduSubject tSubject = twoEduSubjects.get(t);//获取每个二级分类
                if (tSubject.getParentId().equals(eduSubject.getId())) {
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(tSubject, twoSubject);
                    twoFinalSubjectList.add(twoSubject);
                }
            }
            //把一级分类下面所有二级分类放到一级分类里面
            oneSubject.setChildren(twoFinalSubjectList);
        }
        return finalSubjectList;
    }
}
